package com.shujia.spark

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo13Sort {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf()
      .setMaster("local")
      .setAppName("Demo10Union")


    val sc = new SparkContext(conf)

    val studentRDD: RDD[String] = sc.textFile("data/students.txt")

    /**
      *
      * sortBy: 指定一个排序的列，默认是升序
      * ascending 控制排序方式
      */

    val sortByRDD: RDD[String] = studentRDD.sortBy(student => {
      val age: Int = student.split(",")(2).toInt
      age
    }, ascending = false)

    sortByRDD.foreach(println)


    /**
      * sortByKey: 通过key进行排序，默认是升序
      *
      */

    val agesRDD: RDD[(Int, String)] = sc.makeRDD(List((24, "002"), (25, "003")))

    val sortByKeyRDD: RDD[(Int, String)] = agesRDD.sortByKey()

    sortByKeyRDD.foreach(println)

  }

}
